local huajin = fk.CreateSkill{
    name = "kpaI__huajin",
    tags={Skill.Compulsory}
}

Fk:loadTranslationTable{
  ["kpaI__huajin"] = "化烬",
  [":kpaI__huajin"] = "锁定技，结束阶段，你将手牌数调整至你上次失去的牌数，若变化量不为1，你对一名角色造成1点火焰伤害，若你摸牌，改为对自己造成。",

  ["$kpaI__huajin1"] = "杀身有地，报国有时。",
  ["$kpaI__huajin2"] = "益国之事，虽死弗避。",

  ["#kpaI__huajin1"] = "化烬：将手牌弃至 %arg 张",
  ["#kpaI__huajin2"] = "化烬：将手牌弃至 %arg 张，然后对一名角色造成1点火焰伤害",
  ["#kpaI__huajin_damage"] = "对一名角色造成1点火焰伤害",
  ["@kpaI__huajin_last_lost"] = "化烬",
}

huajin:addEffect(fk.EventPhaseStart,{
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(huajin.name) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tarnumb = player:getMark("@kpaI__huajin_last_lost")
    local curnumb = #player:getCardIds("h")
    --
    if tarnumb == curnumb then
      return
    elseif tarnumb > curnumb then
      --摸牌
      player:drawCards(tarnumb-curnumb, huajin.name)
      if tarnumb - curnumb > 1 then
        --打自己
        room:damage({
          from = player,
          to = player,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = huajin.name,
        })
      end
    elseif tarnumb < curnumb then
      --弃牌
      local text = "#kpaI__huajin1:::"
      if curnumb - tarnumb > 1 then
        text = "#kpaI__huajin2:::"
      end
      room:askToDiscard(player,{
        skill_name = huajin.name,
        min_num = curnumb - tarnumb,
        max_num = curnumb - tarnumb,
        include_equip = false,
        prompt = text .. tarnumb,
        cancelable = false,
      })
      if curnumb - tarnumb > 1 then
        --打人
        local chooseplayer = room:askToChoosePlayers(player,{
          targets = room:getAlivePlayers(),
          min_num = 1,
          max_num = 1,
          skill_name = huajin.name,
          prompt = "#kpaI__huajin_damage",
          cancelable = false,
        })[1]
        room:damage({
          from = player,
          to = chooseplayer,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = huajin.name,
        })
      end
    end
    --
  end,
})

huajin:addEffect(fk.AfterCardsMove,{
  mute = true,
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    for _, move in ipairs(data) do
      if move.from == player then
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local lost = 0
    for _, move in ipairs(data) do
      if move.from == player then
       lost = lost+#move.moveInfo
      end
    end
    room:setPlayerMark(player, "@kpaI__huajin_last_lost", lost)
  end,
})

return huajin